/**
 *   <FileManager Class>
 *   This class is the IDE of the programm.
 *   In this class are all the methods to execute the options in the menues.
 *
 *   Copyright (C) 2011  Fernando Gil
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *   @author  Fernando Gil
 *   @mail    lobho.gil@gmail.com
 *   @company Development Solutions (DS) Software
 *   @date    21-Abril-2011
 *   @modify  21-Abril-2011
 *   @by      Fernando Gil
 *   @see     License GNU GPL v3  (/doc/GNU_GPLv3_EN)
 *   @see     Additional Terms    Point 7.1 on License (/doc/GNU_GPLv3_EN)
 *
 *   @note   Replicate this header in all classes.
 *
 */

package org.dss.gil.util;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;


public class FileManager {

     /*********************************************************************
     ********************* S I N G L E T O N *****************************
     *********************************************************************/
    private static FileManager INSTANCE = null;

    private synchronized static void createInstance() {
        if (INSTANCE == null)
            INSTANCE = new FileManager();
    }

    public static FileManager getInstance() {
        if (INSTANCE == null)
            createInstance();
        return INSTANCE;
    }

    private FileManager(){}

/*Test
    public static void main(String a[]){
        FileManager fm = new FileManager();
        try {
            fm.writeTextFile("HOLA fer","C:/Documents and Settings/francisco.gil/My Documents/uno.txt");
        } catch (IOException ex) {
            Globals.log(null,ex);
        }
    }*/

    public void writeTextFile(String text,String fileAddres) throws IOException {
        FileWriter fw = new FileWriter(fileAddres);
        BufferedWriter bf = new BufferedWriter(fw);
        bf.write(text);
        bf.close();
        fw.close();
    }
    
    public String readTextFile(String fileAddres) throws FileNotFoundException, IOException{
        String text="",line;
        FileReader fr = new FileReader(fileAddres);
        BufferedReader buff = new BufferedReader(fr);
        while((line=buff.readLine())!=null){
            text += line;
            text += "\n";
        }
        buff.close();
        fr.close();
        return text;
    }
    
    /** Return every line in a position*/
    public ArrayList <String> readTextFileAsArray(String fileAddres) throws FileNotFoundException, IOException{
        ArrayList <String> list = new ArrayList();
        String line;
        FileReader fr = new FileReader(fileAddres);
        BufferedReader buff = new BufferedReader(fr);
        while((line=buff.readLine())!=null){
            list.add(line);
        }
        buff.close();
        fr.close();
        return list;
    }
    
     /* Recibe una URL, obtiene su Flujo de Datos (Stream) y lo utiliza para leer
      el contenido con un Buffer. Regresa un String con el contenido del archivo.
      La llamada seria:
      URL ejemplo = getClass().getResource("/Library/coDB.gil");
      readTextStream(ejemplo);*/
    public String readTextStream(URL url) throws IOException{
        InputStream is = url.openStream();
        InputStreamReader isr = new java.io.InputStreamReader(is, "UTF-8");
        String text="",line;
        BufferedReader buff = new BufferedReader(isr);
        while((line=buff.readLine())!=null){
            text += line;
            text += "\n";
        }
        buff.close();
        isr.close();
        is.close();
        return text;
    }

}